<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../../../../assets/libs/jquery/jquery-3.6.1.min.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    body {
      width: 100vw;
      height: 200vh;
      background-image: linear-gradient(black, white);
    }

    .container {
      width: 200px;
      height: 200px;
      overflow: auto;
      background-color: #fff;
    }

    div>div {
      width: 100%;
      height: 400px;
      background-image: linear-gradient(#17f, white);
    }
  </style>
</head>

<body>
  <div class="container">
    <div></div>
    <button>回到顶部</button>
  </div>

  <script>
    $(document).scroll(() => {
      // 文档
      console.log(
        $(document).scrollTop(),
        $(document).scrollLeft()
      )
    })

    $('.container').scroll(() => {
      // 元素
      console.log(
        $('.container').scrollTop(),
        $('.container').scrollLeft()
      )
    })

    $('button').click(() => {
      // 滚动到指定位置
      $('.container').scrollTop(0)
      $('.container').scrollLeft(0)
    })
  </script>
</body>

</html>